﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;
using YJKRevitKernel.ParameterEditUtility;
using YJKCopyModel.InteractiveOperation;

namespace YJKCopyModel.RoomTransModel
{
	public class RoomManager
	{
		public static Dictionary<RoomConfigInfo, List<TextInfo>> GroupRoom(List<TextInfo> lstText)
		{
			Dictionary<RoomConfigInfo, List<TextInfo>> dictionary = new Dictionary<RoomConfigInfo, List<TextInfo>>();
			try
			{
				List<string> refName = RoomManager.GetRefName();
				refName.Sort(delegate(string x, string y)
				{
					bool flag2 = x == y;
					int result;
					if (flag2)
					{
						result = 0;
					}
					else
					{
						bool flag3 = x.Length > y.Length;
						if (flag3)
						{
							result = -1;
						}
						else
						{
							result = 1;
						}
					}
					return result;
				});
				Dictionary<TextInfo, string> dictionary2 = new Dictionary<TextInfo, string>();
				using (List<TextInfo>.Enumerator enumerator = lstText.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						TextInfo item = enumerator.Current;
						string text = refName.Find((string name) => RoomManager.IsPointNameRoom(item.UseText, name));
						bool flag = text.IsEmpty<char>();
						if (!flag)
						{
							dictionary2.Add(item, text);
						}
					}
				}
				Dictionary<string, List<TextInfo>> dictionary3 = (from item in dictionary2
				group item by item.Value).ToDictionary((IGrouping<string, KeyValuePair<TextInfo, string>> item) => item.Key, (IGrouping<string, KeyValuePair<TextInfo, string>> item) => (from ThisItem in item
				select ThisItem.Key).ToList<TextInfo>());
				foreach (KeyValuePair<string, List<TextInfo>> keyValuePair in dictionary3)
				{
					dictionary.Add(new RoomConfigInfo
					{
						RoomName = keyValuePair.Key
					}, keyValuePair.Value);
				}
			}
			catch (Exception)
			{
			}
			return dictionary;
		}

		public static RoomTagType CreateRoomTagType(Autodesk.Revit.DB.Document doc, string strName)
		{
			RoomTagType roomTagType = null;
			Transaction transaction = new Transaction(doc, RoomDefaultSet.TransformNameLoadRoomTagFamily);
			try
			{
				List<RoomTagType> roomTagTypeInDoc = RoomManager.GetRoomTagTypeInDoc(doc);
				bool flag = strName == "<程序自选>";
				if (flag)
				{
					string strTypeName = RoomDefaultSet.DefaultRoomTagTypeName;
					roomTagType = roomTagTypeInDoc.Find((RoomTagType item) => item.Name == strTypeName);
					bool flag2 = roomTagType == null;
					if (flag2)
					{
						transaction.Start();
						string text = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(doc), "RoomTag", RoomDefaultSet.DefaultRoomTagFamilyName);
						FamilySymbol familySymbol = null;
                        doc.LoadFamilySymbol(text, strTypeName, new FamilyOptions(), out familySymbol);
						roomTagType = (familySymbol as RoomTagType);
						transaction.Commit();
					}
				}
				else
				{
					roomTagType = roomTagTypeInDoc.Find((RoomTagType item) => RoomManager.FormatRoomTagTypeName(item) == strName);
				}
			}
			catch (Exception)
			{
				bool flag3 = transaction.HasStarted();
				if (flag3)
				{
					transaction.RollBack();
				}
			}
			return roomTagType;
		}

		public static List<RoomNameRef> GetRoomNameRefFromFile(string strPath = "")
		{
			List<RoomNameRef> list = new List<RoomNameRef>();
			try
			{
				string text = strPath;
				bool flag = text.IsEmpty<char>();
				if (flag)
				{
					text = RoomDefaultSet.RoomNameRefFilePath;
				}
				List<RoomNameRef> collection = new List<RoomNameRef>();
				XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<RoomNameRef>));
				bool flag2 = File.Exists(text);
				if (flag2)
				{
					using (FileStream fileStream = File.Open(text, FileMode.Open))
					{
						collection = (xmlSerializer.Deserialize(fileStream) as List<RoomNameRef>);
						list.AddRange(collection);
					}
				}
				else
				{
					list.Add(new RoomNameRef
					{
						Kind = RoomNameKind.eBusiness,
						Name = "办公"
					});
					list.Add(new RoomNameRef
					{
						Kind = RoomNameKind.eCustom,
						Name = "自定义"
					});
					using (FileStream fileStream2 = File.Create(text))
					{
						xmlSerializer.Serialize(fileStream2, list);
					}
				}
			}
			catch (Exception ex)
			{
			}
			return list;
		}

		public static void SaveRoomNameRefInFile(List<RoomNameRef> lstRoomNameRef, string strPath = "")
		{
			try
			{
				string text = strPath;
				bool flag = text.IsEmpty<char>();
				if (flag)
				{
					text = RoomDefaultSet.RoomNameRefFilePath;
				}
				XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<RoomNameRef>));
				bool flag2 = File.Exists(text);
				if (flag2)
				{
					File.Delete(text);
				}
				using (FileStream fileStream = File.OpenWrite(text))
				{
					xmlSerializer.Serialize(fileStream, lstRoomNameRef);
				}
			}
			catch (Exception ex)
			{
			}
		}

		public static List<RoomTagType> GetRoomTagTypeInDoc(Autodesk.Revit.DB.Document doc)
		{
			List<RoomTagType> list = new List<RoomTagType>();
			try
			{
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
				IList<RoomTagType> list2 = filteredElementCollector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_RoomTags).Cast<RoomTagType>().ToList<RoomTagType>();
				foreach (RoomTagType item in list2)
				{
					list.Add(item);
				}
			}
			catch (Exception)
			{
			}
			return list;
		}

		public static string FormatRoomTagTypeName(RoomTagType type)
		{
			return type.FamilyName + ":" + type.Name;
		}

		public static void AddRoomShareParameters(Room room, UIApplication inputApp)
		{
			bool flag = room == null;
			if (!flag)
			{
				try
				{
					string strShareParameterPath = Path.Combine(Product.DataLocation, "YArchitech Shared Parameters_CN.txt");
					string parameterGroup = "房间编号数据";
					List<BuiltInCategory> list = new List<BuiltInCategory>();
					list.Add((BuiltInCategory)(room.Category.Id.IntegerValue));
					SharedParameterEditor.CreateSharedParams(inputApp, strShareParameterPath, "换行文字", parameterGroup, list, false);
					SharedParameterEditor.CreateSharedParams(inputApp, strShareParameterPath, "房间编号", parameterGroup, list, false);
					SharedParameterEditor.CreateSharedParams(inputApp, strShareParameterPath, "面积系数", parameterGroup, list, false);
					SharedParameterEditor.CreateSharedParams(inputApp, strShareParameterPath, "户型名称", parameterGroup, list, false);
					SharedParameterEditor.CreateSharedParams(inputApp, strShareParameterPath, "户型编号", parameterGroup, list, false);
					SharedParameterEditor.CreateSharedParams(inputApp, strShareParameterPath, "单元", parameterGroup, list, false);
					SharedParameterEditor.CreateSharedParams(inputApp, strShareParameterPath, "楼号", parameterGroup, list, false);
					SharedParameterEditor.CreateSharedParams(inputApp, strShareParameterPath, "人员密度", parameterGroup, list, false);
					SharedParameterEditor.CreateSharedParams(inputApp, strShareParameterPath, "前缀", parameterGroup, list, false);
					SharedParameterEditor.CreateSharedParams(inputApp, strShareParameterPath, "后缀", parameterGroup, list, false);
					SharedParameterEditor.CreateSharedParams(inputApp, strShareParameterPath, "房间编号面积单位", parameterGroup, list, false);
				}
				catch (Exception)
				{
				}
			}
		}

		private static List<string> GetRefName()
		{
			List<RoomNameRef> roomNameRefFromFile = RoomManager.GetRoomNameRefFromFile("");
			List<string> list = new List<string>();
			foreach (RoomNameRef roomNameRef in roomNameRefFromFile)
			{
				list.Add(roomNameRef.Name);
			}
			return list;
		}

		private static bool IsPointNameRoom(string strText, string strName)
		{
			bool flag = strText.IsEmpty<char>() || strName.IsEmpty<char>();
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				bool flag2 = !strText.Contains(strName);
				if (flag2)
				{
					result = false;
				}
				else
				{
					bool flag3 = strText.Length - strName.Length > 10;
					result = !flag3;
				}
			}
			return result;
		}
	}
}
